package com.prs.crm.service.cdxh.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.prs.crm.dao.cdxh.DistributionReturnDao;
import com.prs.crm.domain.cdxh.MaterialProduct;
import com.prs.crm.domain.cdxh.ReDispation;
import com.prs.crm.domain.cdxh.SaleOrder;
import com.prs.crm.domain.cdxh.SaleOrderDetail;
import com.prs.crm.domain.cdxh.SerialNumberss;
import com.prs.crm.domain.cdxh.distribution.DistributionReturn;
import com.prs.crm.domain.cdxh.distribution.DistributionReturnDetail;
import com.prs.crm.domain.cdxh.material.MaterialTeam;
import com.prs.crm.service.cdxh.DistributionReturnDetailService;
import com.prs.crm.service.cdxh.DistributionReturnService;
import com.prs.crm.service.cdxh.MaterialProductService;
import com.prs.crm.service.cdxh.MaterialTeamService;
import com.prs.crm.service.cdxh.ReDispationService;
import com.prs.crm.service.cdxh.SaleOrderService;
import com.prs.crm.service.cdxh.SerialNumberssService;
import com.prs.crm.service.impl.BaseServiceImpl;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2011-1-10 上午10:32:16 简单说明
 */

public class DistributionReturnServiceImpl extends
		BaseServiceImpl<DistributionReturn, DistributionReturnDao> implements
		DistributionReturnService {
	private DistributionReturnService distributionReturnService;

	private DistributionReturnDetailService distributionReturnDetailService;

	private ReDispation reDispation;

	private ReDispationService reDispationService;

	private SerialNumberssService serialNumberssService;

	private SaleOrderService saleOrderService;

	private MaterialProductService materialProductService;

	private MaterialTeamService materialTeamService;

	@Override
	public void setReturnCandal(DistributionReturn distributionReturn,
			String[] nums, String[] idss, String[] proCodes, String[] proNames,
			String[] proNorms, String[] proColors, String[] proPacks,String[] pser) {
		/**
		 * 打回配送单设置
		 */
		DistributionReturn DIS = this.getDistributionReturnService().get(
				distributionReturn.getId());
		DIS.setRedisSate(2);
		this.getDistributionReturnService().saveOrUpdis(DIS);
		int lengths = idss.length;
		for (int x = 0; x < lengths; x++) {
			DistributionReturnDetail drd = this.getDistributionReturnDetailService().get(
							Integer.parseInt(idss[x]));
			drd.setDisReturnNums(Integer.parseInt(nums[x]));
			this.getDistributionReturnService().saveOrUpdisdetail(drd);
		}
		/**
		 * 销售订单设置
		 */
		SaleOrder so = this.getSaleOrderService().getSaleOrder(
				distributionReturn.getReOrderCode());
		if (so != null) {
			for (int i = 0; i < lengths; i++) {
				SaleOrderDetail sod = this.getSaleOrderService()
						.getSaleOrderDetail(proCodes[i], so);
				sod.setProDefective(Integer.parseInt(nums[i]));
				this.getSaleOrderService().saveOrudetail(sod);
			}
		}
		/**
		 * 新加一个生成单据
		 */
		for (int y = 0; y < lengths; y++) {
			int gv = Integer.parseInt(nums[y]);
			if (gv > 0) {
				MaterialTeam mtt = this.getMaterialTeamService()
						.getMaterialTeam(proCodes[y],
								distributionReturn.getReOrderCode());
				String Scal = mtt.getMaterialScal();
				MaterialProduct mpObject = new MaterialProduct();
				mpObject.setProductCode(proCodes[y]);
				mpObject.setProductName(proNames[y]);
				mpObject.setProductColor(proColors[y]);
				mpObject.setProductNorm(proNorms[y]);
				mpObject.setDeliveryDate(new Date());
				mpObject.setProductNum(gv);
				mpObject.setPlanOrOrderId(setOrderCodes(distributionReturn
						.getReOrderCode()));
				mpObject.setProductPackstyle(proPacks[y]);
				mpObject.setProductScal(Scal);
				mpObject.setNeedNum(getUj(Scal, proNorms[y], nums[y]));
				mpObject.setRushOrder("否");
				mpObject.setpLState(1);
				mpObject.setAlreadyProduct(0.000);
				mpObject.setFirstYN(1);
				mpObject.setRemainderNum(0.000);
				mpObject.setBackMakeState(3);
				mpObject.setNumConfirmState(1);
				this.getMaterialProductService().save(mpObject);
				//打回扣绩效处理
				
				String sname[]=pser[y].split(",");
				for(String Tname:sname){
					String sec[]=Tname.split("/");
					String xx[]=sec[1].split("-");
					
					String norsz[]=proNorms[y].split("\\*");
					double Number=0.0;
					if("YS".equals(xx[1])){
						Number=((Double.parseDouble(nums[y])/sname.length)/1000)*Double.parseDouble(norsz[1]);
						}else{
						Number=Double.parseDouble(nums[y])/sname.length;
						}
					
						ReDispation  ReD=new ReDispation();	
						ReD.setReDate(getCurrentDate());
						ReD.setOprice(xx[0]);
						ReD.setTkNumbers(Number+"");
						ReD.setProName(sec[0]);
						if("PL".equals(xx[1])){
						ReD.setDepatments("配料组");	
						}
						else if("YS".equals(xx[1])){
						ReD.setDepatments("印刷组");
						}
                        else if("CX".equals(xx[1])){
                        ReD.setDepatments("成型组");
						}
						
					this.getReDispationService().save(ReD)	;
					
					
				}
				
				
				
			
			}
		}
	
		

	}

	@Override
	public void saveOrUpdis(DistributionReturn distributionReturn) {
		this.getDao().saveOrUpdis(distributionReturn);

	}

	@Override
	public void saveOrUpdisdetail(
			DistributionReturnDetail distributionReturnDetail) {
		this.getDao().saveOrUpdisdetail(distributionReturnDetail);

	}

	public DistributionReturnService getDistributionReturnService() {
		return distributionReturnService;
	}

	public void setDistributionReturnService(
			DistributionReturnService distributionReturnService) {
		this.distributionReturnService = distributionReturnService;
	}

	public DistributionReturnDetailService getDistributionReturnDetailService() {
		return distributionReturnDetailService;
	}

	public void setDistributionReturnDetailService(
			DistributionReturnDetailService distributionReturnDetailService) {
		this.distributionReturnDetailService = distributionReturnDetailService;
	}

	public SaleOrderService getSaleOrderService() {
		return saleOrderService;
	}

	public void setSaleOrderService(SaleOrderService saleOrderService) {
		this.saleOrderService = saleOrderService;
	}

	public MaterialProductService getMaterialProductService() {
		return materialProductService;
	}

	public void setMaterialProductService(
			MaterialProductService materialProductService) {
		this.materialProductService = materialProductService;
	}

	public MaterialTeamService getMaterialTeamService() {
		return materialTeamService;
	}

	public void setMaterialTeamService(MaterialTeamService materialTeamService) {
		this.materialTeamService = materialTeamService;
	}

	double getUj(String saqus, String norm, String nums) {
		String ss[] = norm.split("\\*");
		double rU = (Double.parseDouble(saqus) * Double.parseDouble(ss[0])
				* Double.parseDouble(ss[1]) * Double.parseDouble(nums)) / 1000;
		return rU;

	}

	/**
	 * 
	 * @param codes
	 * @return
	 */
	String setOrderCodes(String codes) {
		String op = "";
		SerialNumberss snObject = this.getSerialNumberssService()
				.getSerialNumberssDis(codes);
		if (snObject == null) {
			SerialNumberss snmm = new SerialNumberss();
			snmm.setStyCategory("1LH");
			snmm.setStyNumberCode(codes);
			snmm.setJudgStyle("PS");
			this.getSerialNumberssService().save(snmm);
			op = "1LH" + codes;
		} else {
			SerialNumberss snmm = new SerialNumberss();
			String zjug = addCodes(snObject.getStyCategory());
			snmm.setStyCategory(zjug);
			snmm.setStyNumberCode(codes);
			snmm.setJudgStyle("PS");
			this.getSerialNumberssService().save(snmm);
			op = zjug + codes;
		}

		return op;

	}

	String addCodes(String dd) {
		String d[] = dd.split("L");
		int s = Integer.parseInt(d[0]) + 1;
		String ds = s + "LH";
		return ds;
	}

	public SerialNumberssService getSerialNumberssService() {
		return serialNumberssService;
	}

	public void setSerialNumberssService(
			SerialNumberssService serialNumberssService) {
		this.serialNumberssService = serialNumberssService;
	}

	@Override
	public List<DistributionReturn> getDistributionReturn(String codePlanOrder) {
		// TODO Auto-generated method stub
		return this.getDao().getDistributionReturn(codePlanOrder);
	}

	public ReDispation getReDispation() {
		return reDispation;
	}

	public void setReDispation(ReDispation reDispation) {
		this.reDispation = reDispation;
	}

	public ReDispationService getReDispationService() {
		return reDispationService;
	}

	public void setReDispationService(ReDispationService reDispationService) {
		this.reDispationService = reDispationService;
	}
	public static String getCurrentDate() {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
        int days=cal.get(Calendar.DATE);   
		return days+"/"+month + "/" + year;
	}
}
